<template>
	<Modal v-model="modal" @on-ok="ok('form')" @on-cancel="cancel('form')" :loading="loading">
		<IForm :model="form" ref="form" :label-width="100" :rules="formRule" >
			<IInput v-model="form.id" class="hide" />
	        <FormItem prop="key" label="配置名">
	            <IInput v-model="form.key" size="large" placeholder="配置名、唯一" />
	        </FormItem>
	        <FormItem prop="vue" label="配置值">
	            <IInput v-model="form.vue" size="large" placeholder="配置值" />
	        </FormItem>
	        <FormItem prop="switch" label="开关">
	            <ISwitch v-model="form.switch">
			        <span slot="open">开</span>
			        <span slot="close">关</span>
			    </ISwitch>
	        </FormItem>
	        <FormItem prop="status" label="状态">
	            <IInput v-model="form.status" size="large" placeholder="状态" />
	        </FormItem>
	        <FormItem prop="type" label="类型">
	            <IInput v-model="form.type" size="large" placeholder="类型" />
	        </FormItem>
	        <FormItem prop="desc" label="描述">
	            <IInput v-model="form.desc"  type="textarea" :autosize="{minRows: 5,maxRows: 10}" size="large" placeholder="描述" />
	        </FormItem>
	    </IForm>
	</Modal>
</template>
<script>

	import Modal from 'iview/src/components/modal';
    import Form from 'iview/src/components/form';
    import Input from 'iview/src/components/input';
    import Button from 'iview/src/components/button';
    import Switch from 'iview/src/components/switch';

    import Axios from 'admin/config/axios';
	export default {
		components:{
            Modal,
            'IInput' : Input,
			'IForm' : Form,
			'FormItem' : Form.Item,
			'IButton' : Button,
			'ISwitch' : Switch,
        },
        data () {
            return {
                modal: false,
                loading : true,
                form :{
					id : 0,
					key : '',
					vue  : '',
					switch : false,
					status : '',
					type : '',
					desc : '',

                },
                formRule : {
	                key: [
	                    { required: true, message: '请填写配置名', trigger: 'blur' },
	                ],
	            },
            }
        },
        methods: {
        	init(){
        		this.form.id = 0;
        		this.form.key = '';
        		this.form.vue = '';
        		this.form.switch = false;
        		this.form.status = '';
        		this.form.type = '';
        		this.form.desc = '';
        	},
            changeLoading(){
	            this.loading = false;
	            this.$nextTick(() => {
	              this.loading = true;
	            });
	        },
        	ok(name){
        		this.$refs[name].validate(valid => {
        			if (!valid) {this.changeLoading();return false;}

        			Axios.Configs.Config.Update(this.form)
		            .then(res =>{
		            	this.changeLoading();
		            	if(!res) return false;
		                this.modal = false;
		                this.$refs[name].resetFields();
		                this.init();
		                this.$emit('resetTable');
		            })
		            .catch(error =>{
		                this.$Message.error(error);
		            })
        		})
        	},
        	cancel(name){
        		this.$refs[name].resetFields();
        		this.init();
        	}
        },
    }
</script>